package org.john.guavatest;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.junit.Test;

/**
 * Created by adsame on 17-1-23.
 */
public class TestLoadingCache {
    @Test
    public void TestLoadingCache() throws Exception{
        LoadingCache<String,String> cacheBuilder= CacheBuilder
                .newBuilder()
                .build(new CacheLoader<String, String>(){
                    @Override
                    public String load(String key) throws Exception {
                        String strProValue="hello "+key+"!";
                        return strProValue;
                    }

                });

        System.out.println("jerry value:"+cacheBuilder.getUnchecked("jerry"));
        System.out.println("jerry value:"+cacheBuilder.get("jerry"));
        System.out.println("peida value:"+cacheBuilder.get("peida"));
        System.out.println("peida value:"+cacheBuilder.getUnchecked("peida"));
        System.out.println("lisa value:"+cacheBuilder.getUnchecked("lisa"));
        cacheBuilder.put("harry", "ssdded");
        System.out.println("harry value:"+cacheBuilder.get("harry"));
    }

}
